package com.qifenqian.turbot.module.ui.dish

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.module.adapter.restaurant.DishManagePagerAdapter
import com.qifenqian.turbot.module.contract.DishContract
import com.qifenqian.turbot.module.entity.response.dish.CategoryEntity
import com.qifenqian.turbot.module.entity.response.dish.DishEntity
import com.qifenqian.turbot.module.presenter.DishPresenter
import kotlinx.android.synthetic.main.activity_dish_manage.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2019/1/3 16:08
 * @description 菜品管理,菜品选择
 * @see
 * @since
 */

const val PARAM_ACTION = "action"
const val PARAM_POSITION = "position"

const val ACTION_MANAGE = "dish_manage"
const val ACTION_SPECIAL_DISH_CHOOSE = "特价菜选择"
const val ACTION_DISCOUNT_DISH_CHOOSE = "单品折扣菜选择"

class DishManageActivity : BaseActivity<DishPresenter, DishContract.View>(), DishContract.View {

    companion object {
        /**
         * @param action    从菜品管理还是菜品选择跳转
         * @param position  从菜品选择跳转过来的时候点击的是星期几
         */
        fun startThis(context: Context, action: String, position: Int = -1) {
            val intent = Intent(context, DishManageActivity::class.java)
            intent.putExtra(PARAM_ACTION, action)
            intent.putExtra(PARAM_POSITION, position)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_dish_manage
    }

    override fun bindPresenter(): DishPresenter? {
        return DishPresenter(this, this)
    }

    private var mAction: String = ACTION_SPECIAL_DISH_CHOOSE
    fun getAction(): String {
        return mAction
    }

    private var mPosition: Int = -1
    fun getPosition(): Int {
        return mPosition
    }

    private var mCurrentPage: Int = -1
    fun getCurrentPage(): Int {
        return mCurrentPage
    }

    fun setCurrentPage(currentPage: Int) {
        mCurrentPage = currentPage
    }

    private var mCategoryDishes = mutableListOf<MutableList<DishEntity>>()
    private var mDishEntities = mutableListOf<DishEntity>()
    private var mCategoryEntities = mutableListOf<CategoryEntity>()
    private var mFragmentList = mutableListOf<TabDishesFragment>()
    private var mDishManagePagerAdapter: DishManagePagerAdapter? = null

    override fun initial() {
        mAction = intent.getStringExtra(PARAM_ACTION)
        mPosition = intent.getIntExtra(PARAM_POSITION, -1)
        tl_dish_manage.setupWithViewPager(vp_dish_manage)
        if (mAction == ACTION_MANAGE) {
            tv_dish_manage_title.text = "菜品管理"
            btn_dish_manage_edit.visibility = View.VISIBLE
            btn_dish_manage_category_add.visibility = View.VISIBLE
        } else if (mAction == ACTION_SPECIAL_DISH_CHOOSE) {
            tv_dish_manage_title.text = "菜品选择"
            btn_dish_manage_edit.visibility = View.INVISIBLE
            btn_dish_manage_category_add.visibility = View.INVISIBLE
        }
        btn_dish_manage_edit.setOnClickListener {
            // 2018/12/24 跳转到菜系编辑界面 ----->
            DishCategoryManageActivity.startThis(this)
        }
        btn_dish_manage_category_add.setOnClickListener {
            showCategoryAddDialog()
        }
    }

    override fun onResume() {
        super.onResume()
        mPresenter!!.getDishAndCategoryList()
    }

    private var mDialogAddCategory: Dialog? = null
    private fun showCategoryAddDialog() {
        mDialogAddCategory = Dialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null)
        mDialogAddCategory!!.setContentView(view)
        mDialogAddCategory!!.show()
        view.tv_dialog_edit_title.text = "添加菜品分类"
        view.btn_dialog_edit_cancel.setOnClickListener { mDialogAddCategory!!.dismiss() }
        view.btn_dialog_edit_ok.setOnClickListener {
            if (view.et_dialog_edit_content.text.toString().isEmpty()) {
                toast("输入内容不能为空")
                return@setOnClickListener
            }
            for (categoryEntity in mCategoryEntities) {
                if (categoryEntity.dishTypeName == view.et_dialog_edit_content.text.toString()) {
                    toast("该类型已存在")
                    return@setOnClickListener
                }
            }
            mPresenter!!.addCategory(view.et_dialog_edit_content.text.toString())
        }
    }

    override fun getDishAndCategoryListResult(isSuccess: Boolean, code: Int, msg: String?, categoryEntities: List<CategoryEntity>?, dishEntities: List<DishEntity>?) {
        if (isSuccess) {
            if (categoryEntities != null) {
                mCategoryEntities.clear()
                mCategoryEntities.addAll((categoryEntities as MutableList<CategoryEntity>?)!!)
            }
            if (dishEntities != null) {
                mDishEntities.clear()
                mDishEntities.addAll((dishEntities as MutableList<DishEntity>?)!!)
            }
            mCategoryDishes.clear()
            mFragmentList.clear()
            for (category in mCategoryEntities) {
                val tabDishesFragment = TabDishesFragment()
                mFragmentList.add(tabDishesFragment)
                val tabDishes = arrayListOf<DishEntity>()
                for (dishEntity in mDishEntities) {
                    if (dishEntity.dishTypeId == category.id) {
                        tabDishes.add(dishEntity)
                    }
                }
                mCategoryDishes.add(tabDishes)
            }
            mDishManagePagerAdapter = DishManagePagerAdapter(supportFragmentManager, mCategoryEntities, mFragmentList)
            vp_dish_manage.adapter = mDishManagePagerAdapter
            mDishManagePagerAdapter!!.setCategoryDishes(mCategoryDishes, mCategoryEntities)
        } else {
            toast(msg!!)
            finish()
        }
        vp_dish_manage.currentItem = getCurrentPage()
    }

    override fun addCategoryResult(isSuccess: Boolean, code: Int, msg: String?, categoryEntity: CategoryEntity?) {
        if (isSuccess) {
//            mCategoryEntities.add(categoryEntity!!)
//            mFragmentList.add(TabDishesFragment())
//            mDishManagePagerAdapter!!.notifyDataSetChanged()
            mPresenter!!.getDishAndCategoryList()
            mDialogAddCategory!!.dismiss()
        }
        toast(msg!!)
    }

}
